const tree = [

    { id: 1, name: 'boss', parentId: 0 },

    { id: 2, name: 'lily', parentId: 1 },

    { id: 3, name: 'jack', parentId: 1 },

    { id: 4, name: 'john', parentId: 2 },

    { id: 5, name: 'boss2', parentId: 0 },

]

function treeData(tree) {
    let cloneData = JSON.parse(JSON.stringify(tree));
    return cloneData.filter((father) => {
        let res = cloneData.filter((child) =>
            child.parentId == father.id
        )
        res.length > 0 ? father.child = res : '';
        return father.parentId == 0;
    })
}

// function treeData(tree) {
//     let cloneData = JSON.parse(JSON.stringify(tree));
//     return cloneData.filter((father) => {
//         let barry = cloneData.filter((child) =>
//             child.parentId == father.id
//         )
//         barry.length > 0 ? father.child = barry : '';
//         return father.parentId == 0;
//     })
// }

var newTree = treeData(tree);
console.log(newTree);


//先想 返回第一层的格式 然后再想如何添加孩子